package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.bean.gen.ErpMerchandiseType;
import cn.jekun.component.core.service.gen.ErpMerchandiseTypeService;

/**
 * 商品分类管理
 * 
 */
public class MerchandiseTypeAction extends ShopBaseAction {
	private static final String AUTH_SHOP_INVENTORY_MANAGER="SHOP_INVENTORY_MANAGER";//库存管理
	
	@Resource(name=ErpMerchandiseTypeService.BEAN_NAME)
	ErpMerchandiseTypeService erpMerchandiseTypeService;
	
	private ErpMerchandiseType erpMerchandiseType;//商品分类数据
	private String typeLevel;//分类级别
	private String typeType;//分类类型
	private String typeKeyType;//分类关键字类型
	private String typeKeyWord;//分类关键字

	public String getTypeKeyType() {
		return typeKeyType;
	}

	public void setTypeKeyType(String typeKeyType) {
		this.typeKeyType = typeKeyType;
	}

	public String getTypeKeyWord() {
		return typeKeyWord;
	}

	public void setTypeKeyWord(String typeKeyWord) {
		this.typeKeyWord = typeKeyWord;
	}

	public String getTypeType() {
		return typeType;
	}

	public void setTypeType(String typeType) {
		this.typeType = typeType;
	}

	public String getTypeLevel() {
		return typeLevel;
	}

	public void setTypeLevel(String typeLevel) {
		this.typeLevel = typeLevel;
	}

	public ErpMerchandiseType getErpMerchandiseType() {
		return erpMerchandiseType;
	}

	public void setErpMerchandiseType(ErpMerchandiseType erpMerchandiseType) {
		this.erpMerchandiseType = erpMerchandiseType;
	}
	
	
	/**
	 * 查询商品列表
	 */
	@SuppressWarnings("unchecked")
	public String getMerchandTypeList(){
		String shopId=this.getSessionUser().getShopId();
		this.checkRight(AUTH_SHOP_INVENTORY_MANAGER);//检查权限
		if(StringUtils.isBlank(shopId)){
			this.saveActionError("商户编码不能为空");
			return "/shop/goodsTypeManagement.jsp";
		}
		String typeLevSql = "" ;
		String typeTypeSql = "";
		String typeKeySql ="";
		if(typeLevel!= null&&typeLevel!=""){//分类级别
			if(typeLevel.equals("first")){//父级分类
				typeLevSql=" AND PARENT_ID IS NULL ";
			}
			if(typeLevel.equals("second")){//下级分类
				typeLevSql=" AND PARENT_ID IS NOT NULL ";
			}
		}
		if(typeType!= null&&!typeType.equals("")){//分类类型
			typeTypeSql=" AND SHOP_ID IS NULL ";//公共分类
		}else{//商家分类
			typeTypeSql=" AND SHOP_ID ="+shopId;
			}
		if(typeKeyType!=null&&!typeKeyType.equals("")){
			if(typeKeyType.equals("typeName")){
				formMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_NAME,typeKeyWord);
				typeKeySql= SqlUtis.getSQL(formMap,"a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_NAME,Types.VARCHAR,SqlUtis.LIKE);
			}
			if(typeKeyType.equals("typeId")){
				formMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,typeKeyWord);
				typeKeySql= SqlUtis.getSQL(formMap,"a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ);
			}
		}
		SqlUtis.prepareSql(formMap,
				typeKeySql,
				typeLevSql,
				typeTypeSql
				);
		resultList = this.erpMerchandiseTypeService.list(formMap,page);
		request.setAttribute("resultInfo", request.getParameter("resultInfo"));
		return "/shop/goodsTypeManagement.jsp";
	}
	/**
	 * 新增商品分类页面
	 */
	public String goodsTypeAddPage(){
		String shopId =this.getSessionUser().getShopId();
		request.setAttribute("shopId",shopId );
		List<ErpMerchandiseType> erpMerchandiseTypesList = getParendTypeId();
		request.setAttribute("erpMerchandiseTypesList",erpMerchandiseTypesList);
		
		request.setAttribute("resultInfo",request.getParameter("resultInfo"));//操作结果
		return "/shop/goodsTypeAdd.jsp";
	}
	/**
	 * 保存新增商品分类
	 */
	@SuppressWarnings({"unchecked" })
	public String goodsTypeAdd(){
		erpMerchandiseType.setShopId(this.getSessionUser().getShopId());
		formMap.put("erpMerchandiseType", erpMerchandiseType);
		String resultId = this.erpMerchandiseTypeService.insert(formMap).toString();
		int resultInfo;
		if(resultId==null){
			resultInfo=0;
		}else{
			resultInfo=1;
		}
		return "!/shop/MerchandiseType/goodsTypeAddPage.do?resultInfo="+resultInfo;
	}
	/**
	 * 查询公共商品分类详情
	 */
	@SuppressWarnings({ "unchecked" })
	public String getGoodsTypeCommonDetail(){
		String goodsTypeId = request.getParameter("goodsTypeId");
		formMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,goodsTypeId);
		SqlUtis.prepareSql(formMap, 
				SqlUtis.getSQL(formMap, "a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID, Types.VARCHAR,SqlUtis.EQ));
		this.erpMerchandiseType = erpMerchandiseTypeService.one(formMap);
		request.setAttribute("erpMerchandiseType", erpMerchandiseType);//商品分类详情
		
		List<ErpMerchandiseType> erpMerchandiseTypesList = getParendTypeId();
		String shopId =this.getSessionUser().getShopId();
		request.setAttribute("shopId",shopId );
		request.setAttribute("erpMerchandiseTypesList",erpMerchandiseTypesList);//查询父类代码 包括公共分类和商家商品分类
		request.setAttribute("resultInfo",request.getParameter("resultInfo"));//操作结果
		return "/shop/goodsTypeCommonDetail.jsp";
	}
	/**
	 * 查询商家商品分类详情
	 */
	@SuppressWarnings({ "unchecked" })
	public String getGoodsTypeDetail(){
		String goodsTypeId = request.getParameter("goodsTypeId");
		formMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,goodsTypeId);
		SqlUtis.prepareSql(formMap, 
				SqlUtis.getSQL(formMap, "a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID, Types.VARCHAR,SqlUtis.EQ));
		this.erpMerchandiseType = erpMerchandiseTypeService.one(formMap);
		request.setAttribute("erpMerchandiseType", erpMerchandiseType);//商品分类详情
		
		List<ErpMerchandiseType> erpMerchandiseTypesList = getParendTypeId();
		String shopId =this.getSessionUser().getShopId();
		request.setAttribute("shopId",shopId );
		request.setAttribute("erpMerchandiseTypesList",erpMerchandiseTypesList);//查询父类代码 包括公共分类和商家商品分类
		request.setAttribute("resultInfo",request.getParameter("resultInfo"));//操作结果
		return "/shop/goodsTypeDetail.jsp";
	}
	/**
	 * 保存商品分类详情
	 */
	@SuppressWarnings({"unchecked" })
	public String saveGoodsTypeDetail(){
		erpMerchandiseType.setShopId(this.getSessionUser().getShopId());
		String typeId=erpMerchandiseType.getMerchandiseTypeId();
		formMap.put("erpMerchandiseType", erpMerchandiseType);
		formMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID, erpMerchandiseType.getMerchandiseTypeId());
		SqlUtis.prepareSql(formMap, 
				SqlUtis.getSQL(formMap,"a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ));
		Integer resultInfo = this.erpMerchandiseTypeService.update(formMap);
		return "!/shop/MerchandiseType/getGoodsTypeDetail.do?resultInfo="+resultInfo+"&goodsTypeId="+typeId;
	}
	/**
	 * 删除商品分类
	 */
	@SuppressWarnings({ "unchecked" })
	public String delGoodsType(){
		//检测是否有子级分类
		Integer resultInfo=0;
		formMap.put(ErpMerchandiseType.COL_PARENT_ID, request.getParameter("goodsTypeId").trim());
		SqlUtis.prepareSql(formMap, 
				SqlUtis.getSQL(formMap,"a."+ErpMerchandiseType.COL_PARENT_ID,Types.VARCHAR,SqlUtis.EQ))
				;
		List<ErpMerchandiseType> erpMerchandiseTypeList = erpMerchandiseTypeService.list(formMap);
		if(erpMerchandiseTypeList.size()==0){
			formMap = new HashMap();
			formMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID, request.getParameter("goodsTypeId").trim());
			SqlUtis.prepareSql(formMap, 
					SqlUtis.getSQL(formMap,"a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ))
					;
			resultInfo=erpMerchandiseTypeService.delete(formMap);
		}

		return "!/shop/MerchandiseType/getMerchandTypeList.do?resultInfo="+resultInfo;
	}
	/**
	 * 查询父类代码，包括公共分类和商家商品分类
	 */
	@SuppressWarnings({ "rawtypes" })
	public List<ErpMerchandiseType> getParendTypeId(){
		String shopId =this.getSessionUser().getShopId();
		// 查询父类代码 包括公共分类和商家商品分类
		Map typeMap = new HashMap();
		SqlUtis.prepareSql(typeMap, " AND (a.PARENT_ID IS NULL)", " AND (a.SHOP_ID IS NULL", " OR a.SHOP_ID = '" + shopId + "')");
		List<ErpMerchandiseType> erpMerchandiseTypesList = this.erpMerchandiseTypeService.list(typeMap);
		return erpMerchandiseTypesList;
	}
}
